﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditStateTemplate : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { StateTemplateObject = o as StateTemplate; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(StateTemplate); } }
        public string VisualizerName { get { return "EditStateTemplate"; } }

        public static readonly DependencyProperty StateTemplateObjectProperty = DependencyProperty.Register(
            "StateTemplateObject", typeof(StateTemplate), typeof(EditStateTemplate), new PropertyMetadata(null, new PropertyChangedCallback(StateTemplateChanged)));

        public StateTemplate StateTemplateObject
        {
            get { return (StateTemplate)GetValue(StateTemplateObjectProperty); }
            set { SetValue(StateTemplateObjectProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditStateTemplate, T2> action)
        {
            d.HandleChange<EditStateTemplate, T2>(e, action, (t) => t.UpdateBindings());
        }


        private static void StateTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<StateTemplate>(d, e, (t, val) => t.StateTemplateObject = val);
        }

        public override void UpdateBindings()
        {
            if (gridData != null)
                gridData.DataContext = StateTemplateObject;
        }

        public override string TitleText
        {
            get
            {
                if (StateTemplateObject == null)
                    return string.Empty;

                return string.Format("State Template {0} ({1} Props)", StateTemplateObject.FriendlyName, StateTemplateObject.PropertiesCount);
            }
        }

        public EditStateTemplate()
        {
            InitializeComponent();
        }

        private void basicTemplate_Loaded(object sender, RoutedEventArgs e)
        {
            //editBasicTemplate.wc.Indent = wc.Indent + 1;
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreStateTemplateAsync(DataAccess.AssignedUniqueId, StateTemplateObject);
        }        
    }
}
